<?php
// zde prosím uveďte skutečnou cestu ke složce Nette uvnitř distribuce
//dfs interna classa handlujuca uzly v jobs
class core_dfs extends Object
{
//db connection
public $databaseconn="";
//db table
public $table="";

//constructor simply assign database connection values
function core_dfs($databaseconn, $table)
    {
      $this->databaseconn=$databaseconn;
      $this->table=$table;
    }

//interna funkce prepocita strom
function DFS_MAIN() {
  mysql_query("update ".$this->table." set state='white'",$this->databaseconn);
  mysql_query("update ".$this->table." set left_state=0 where ID=1",$this->databaseconn);
  $right_state=$this->check(1)+1;
  mysql_query("update ".$this->table." set right_state=".$right_state." where ID=1",$this->databaseconn);
  mysql_query("update ".$this->table." set state='black' where ID=1",$this->databaseconn);
  }

function check($level) {
  static $count = 0;
  $result=mysql_query("select * from ".$this->table." where PID=".$level."",$this->databaseconn);
  while ($row=mysql_fetch_array($result)) {
    mysql_query("update ".$this->table." set state='gray' where ID=".$row['ID'],$this->databaseconn);
    $count++;
    mysql_query("update ".$this->table." set left_state=".$count." where ID=".$row['ID'],$this->databaseconn);
    $count=$this->check($row['ID']);
    $count++;
    mysql_query("update ".$this->table." set right_state=".$count." where ID=".$row['ID'],$this->databaseconn);   
    mysql_query("update ".$this->table." set state='black' where ID=".$row['ID'],$this->databaseconn); 
    }
  return($count);
  }





//vstupoju parametre parenta

}

class core_dfs_node extends core_dfs
{
public $PID="";
public $LEVEL="";
public $left="";
public $right="";
public $name="";
public $ID="";
public $disabled="";
public $users="";

function setid($id) {
	$this->ID=$id;
	$result=mysql_query("select * from ".mysql_real_escape_string($this->table)." where id=".mysql_real_escape_string($id)."");
	$row = mysql_fetch_array($result);
	if ($this->name == "") {
		$this->name=$row['name'];	
		}
	if ($this->disabled == "") {
		$this->disabled=$row['disabled'];		
		}
	if ($this->users == "") {
		$this->users=$row['users'];		
		}
	}
	
//vstupuju parametre parenta
function add_validate() {
	$out="";	
	$loc = new loc;
	if ($this->name == "") {
		$out=$out."<br>".$loc->validation_addnode_name_miss; 		
		}
	if ($this->disabled == "") {
		$out=$out."<br>".$loc->validation_editnode_disable_miss; 		
		}
	if ($this->users == "") {
		$out=$out."<br>".$loc->validation_editnode_users_miss; 		
		}
	return $out;
	}

function add() {
	$validation=$this->add_validate();
	if ($validation == "") {
  		$LEVEL=$this->LEVEL+1;
  		$left=$this->left+1;
  		$right=$this->left+2;
  		$query="insert into ".mysql_real_escape_string($this->table)." (PID,LEVEL,name,left_state,right_state,disabled,users) values (".mysql_real_escape_string($this->PID).",".mysql_real_escape_string($LEVEL).",'".mysql_real_escape_string($this->name)."',".mysql_real_escape_string($left).",".mysql_real_escape_string($right).",".mysql_real_escape_string($this->disabled).",'".mysql_real_escape_string($this->users)."')";
		mysql_query($query,$this->databaseconn);
  		//tu sa to da zrychlit prediktivnym prepocitavanim casti stromu o 2
  		$this->DFS_MAIN();
  		}
 	}

function edit_validate() {
   $out="";	
	$loc = new loc;
	if ($this->name == "") {
		$out=$out."<br>".$loc->validation_addnode_name_miss; 		
		}
	if ($this->disabled == "") {
		$out=$out."<br>".$loc->validation_editnode_disable_miss; 		
		}
	if ($this->users == "") {
		$out=$out."<br>".$loc->validation_editnode_users_miss; 		
		}
	if ($this->ID == "") {
		$out=$out."<br>".$loc->validation_editnode_id_miss; 		
		}
	return $out;
	}
	 	

function edit() {
	$validation=$this->edit_validate();
	if ($validation == "") {
		mysql_query("update ".mysql_real_escape_string($this->table)." set name='".mysql_real_escape_string($this->name)."', disabled=".mysql_real_escape_string($this->disabled).", users='".mysql_real_escape_string($this->users)."' where id=".mysql_real_escape_string($this->ID)."");
		//return "update ".mysql_real_escape_string($this->table)." set name='".mysql_real_escape_string($this->name)."', disabled=".mysql_real_escape_string($this->disabled)." users='".mysql_real_escape_string($this->users)."' where id=".mysql_real_escape_string($this->ID).""; 
		}	
	}

//totall override
function set_values_by_id($id) {
 if ($id != "") {
 	$query="select * from tree where ID=".$id."";
 	$this->ID=$id;
 	$result=mysql_query($query);
 	$row=mysql_fetch_array($result);
 	$this->name = $row['name'];
	$this->disabled = $row['disabled'];
	$this->users = $row['users'];
	$this->PID=$id;
  	$this->LEVEL=$row['LEVEL'];
  	$this->left=$row['left_state'];
  	$this->right=$row['right_state'];
 	}
}

function move_subtree_validate($what,$where) {
	$out="";	
	$count=0;
	$loc = new loc;
	if ($what == "" OR $where == "") {
		$out=$out."<br>".$loc->validation_movenode_id_miss; 		
		} else {
		$query="select left_state,right_state from tree where ID = ".$what."";
		$result=mysql_query($query);
		$row=mysql_fetch_array($result);
		//$out=$out."<br>".$query;
		if (isset($row['left_state'])) {
			$query="select ID from tree where left_state > ".$row['left_state']." AND right_state < ".$row['right_state']."";
			$result=mysql_query($query);
			while ($row2=mysql_fetch_array($result)) {
				$children[$count]=$row2['ID'];
				$count++;
				}
			if ($count != 0) {
			if (in_array($where, $children)) {
				$out=$out."<br>".$loc->validation_movenode_cannotmovetochild;
				}}
				
			
			}
		}
	return $out;
	}

function move_subtree($what,$where) {
	$count=0;
	$validation=$this->move_subtree_validate($what,$where);
	if ($validation == "") {
		$query="select * from tree where ID = ".$where."";
		$result=mysql_query($query);
		$wherestats=mysql_fetch_array($result);
		$query="select * from tree where ID = ".$what."";
		$result=mysql_query($query);
		$whatstats=mysql_fetch_array($result);
		$level_delta=$wherestats['LEVEL']+1-$whatstats['LEVEL'];
		$newlevel=$whatstats['LEVEL']+$level_delta;
		$query="update tree set LEVEL = ".$newlevel.", PID = ".$where." WHERE ID = ".$what."";
		mysql_query($query);
		$query="select ID from tree where left_state > ".$whatstats['left_state']." AND right_state < ".$whatstats['right_state']."";
			$result=mysql_query($query);
			while ($row2=mysql_fetch_array($result)) {
				$children[$count]=$row2['ID'];
				$count++;
				}
			if ($count != "0") {		
			foreach ($children as $ID) {
				$query="select LEVEL from tree where ID = ".$ID."";
				$result=mysql_query($query);
				$level=mysql_fetch_array($result);
				$newlevel=$level['LEVEL']+$level_delta;
			
				$query="update tree set LEVEL = ".$newlevel." WHERE ID = ".$ID."";
				mysql_query($query);
				}
				}
		$this->DFS_MAIN();
		}
		
	
	}

}




  ?>
